package com.util;

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;

@SuppressWarnings("serial")
public class DownLoad extends HttpServlet {
  public DownLoad() {
  }

  public void doGet(HttpServletRequest httpServletRequest,
                    HttpServletResponse httpServletResponse) {
    String filePathAndName = httpServletRequest.getParameter("filePathAndName");
    //transport files
    try {
      //httpServletResponse.setContentType("application/exe");
      String fileName = filePathAndName.substring(filePathAndName.lastIndexOf("/")+1);
      httpServletResponse.setHeader("Content-disposition",
                         "attachment; filename=" + fileName);
    System.out.println("fileName: "+fileName);
      ServletOutputStream out = httpServletResponse.getOutputStream();
      FileInputStream in = new FileInputStream(filePathAndName);
      BufferedInputStream bis = new BufferedInputStream(in);
      BufferedOutputStream bos = new BufferedOutputStream(out);
      byte[] buff = new byte[2048];
      int bytesRead;
      while ( -1 != (bytesRead = bis.read(buff, 0, buff.length))) {
        bos.write(buff, 0, bytesRead);
      }
      bos.close();
      bis.close();
    }
    catch (java.io.IOException ioe) {
      System.err.println(ioe.getMessage());
    }
  }
}
